﻿using ESMAM.BLL.SYS;
using ESMAM.Tools;
using ESMAM.Web.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ESMAM.WebApp.Controllers
{
    public class UserController : BaseController
    {
        // GET: User
        public ActionResult Index()
        {
            return View();
            //return View();
        }

        [HttpGet]
        public ActionResult ChangePwd()
        {
            return View(CurrUser);
        }

        [HttpPost]
        public ActionResult ChangePwd(string dia_loginName, string dia_old_pwd, string dia_new_pwd1, string dia_new_pwd2)
        {
            var ret = UserBLL.Login(dia_loginName, dia_old_pwd);
            if (ret.Code == -1)
            {
                return this.JsonBJUI(300, ret.Msg);
            }
            else
            {
                if (dia_new_pwd1.Length < 6)
                {
                    return this.JsonBJUI(300, "新密码长度不能少于6位字符！");
                }

                if (dia_new_pwd1.Length > 20)
                {
                    return this.JsonBJUI(300, "新密码长度不能大于20位字符！");
                }

                if (dia_new_pwd1 != dia_new_pwd2)
                {
                    return this.JsonBJUI(300, "新密码不一致，修改密码失败！");
                }
                else
                {
                    ret = UserBLL.ChangePassWd(CurrUser.id.ToString(), dia_new_pwd1);
                    if (ret.Code == -1)
                    {
                        return this.JsonBJUI(300, ret.Msg);
                    }
                    return this.JsonBJUI(200, ret.Msg);
                }
            }
        }


        [HttpPost]
        public ActionResult UserSearch(int pageCurrent, int pageSize, string orders)
        {
            var ret = UserBLL.GetPager(Request.Form);
            if (ret.Code == -1)
            {
                return this.JsonBJUI(300, ret.Msg);
            }
            Pager pager = ret.Value;
            return this.JsonNet(pager);
        }

        [HttpPost]
        public void UserExport()
        {
            var ret = UserBLL.SelectTable(Request.Form);
            if (ret.Code == -1)
            {
                return;
            }
            DataTable data = ret.Value;
            string json = Request.Form["mappingNames"];
            if (string.IsNullOrWhiteSpace(json))
            {
                return;
            }
            var lstSetting = json.ToObject<List<ExportSetting>>();
            string fileName = Server.UrlPathEncode(Request.Form["fileName"] + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx");
            ExcelUtils.ExportlExcel(lstSetting, data, true, fileName);
        }

        [HttpGet]
        public ActionResult Edit()
        {
            return View(CurrUser);
        }

        [HttpPost]
        public ActionResult UserUpsert()
        {
            var ret = UserBLL.Upsert(Request.Form);
            if (ret.Code == -1)
            {
                return this.JsonBJUI(300, ret.Msg);
            }
            if (Request.Form["Id"] == CurrUser.id.ToString()) //当前用户的信息做了修改，则需要更新 session 中的 用户关联所属的html
            {
                Session[CONST_SESSION_USER] = UserBLL.GetUser(Request.Form["Id"]).Value;
            }

            if (CurrUser.shiyyy != CurrLang)
            {
                CookieHelper.SetCookie(BaseController.CONST_COOKIE_LANG, CurrUser.shiyyy + "", DateTime.Now.AddDays(15));
            }

            return this.JsonBJUI(200, ret.Msg);
        }

        [HttpPost]
        public ActionResult DelUser(string id)
        {
            var ret = UserBLL.Delete(id);
            if (ret.Code == -1)
            {
                return this.JsonBJUI(300, ret.Msg);
            }
            return this.JsonBJUI(200, "删除成功！");
        }
    }
}